package com.example.zhuwenjing

import Student
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity2 : AppCompatActivity() {
    private val students = mutableListOf<Student>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        val studentIdEditText = findViewById<EditText>(R.id.studentId)
        val studentNameEditText = findViewById<EditText>(R.id.studentName)
        val studentSexEditText = findViewById<EditText>(R.id.studentSex)
        val studentClassEditText = findViewById<EditText>(R.id.studentClass)
        val confirmButton = findViewById<Button>(R.id.button4)
        val cancelButton = findViewById<Button>(R.id.button3)

        confirmButton.setOnClickListener {
            val studentId = studentIdEditText.text.toString()
            val studentName = studentNameEditText.text.toString()
            val studentSex = studentSexEditText.text.toString()
            val studentClass = studentClassEditText.text.toString()
            val newStudent = Student(studentId, studentName, studentSex, studentClass)
            students.add(newStudent)
            val resultIntent = Intent()
            resultIntent.putParcelableArrayListExtra("newStudents", ArrayList(students))
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }

        cancelButton.setOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            finish()
        }
    }
}